package com.hy.main.demo.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Author yang DongYong
 * @Date 2022/5/11 - 05 - 11 - 2:17
 * @Description: com.hy.main.demo.rabbitmq
 * @version: 1.0
 */
public class DirectLogs01 {
    public static final String EXCHANGE_NAME = "direct_logs";
    public static final String QUEUE_NAME = "console";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        // 队列绑定交换机
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "warning");
        // 消息成功回调
        DeliverCallback deliverCallback = (tag, message) -> System.out.println(new String(message.getBody()));
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> System.out.println("消息取消"));
    }
}

class DirectLogs02 {
    public static final String EXCHANGE_NAME = "direct_logs";
    public static final String QUEUE_NAME = "disk";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        // 队列绑定交换机
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "Error");
        // 消息成功回调
        DeliverCallback deliverCallback = (tag, message) -> System.out.println("接收到的消息:" + new String(message.getBody()));
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> System.out.println("消息取消"));
    }
}
